package com.appcenter.jz.unit;

import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ObjectUnit {

    public static void Copy(Object source, Object dest) throws Exception {
        BeanUtils.copyProperties(source,dest);
    }


    public static String MD5Encryption(String text) {
        String result = text+"8966877"+"Coke";
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(result.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            re_md5 = buf.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return re_md5.toUpperCase();
    }


    public static <T1,T2> List<Object> CopyList2(List<T1> oldList,Object clazz) throws IntrospectionException, InvocationTargetException, IllegalAccessException, InstantiationException {
        List<Object> newList = new ArrayList<Object>();
        if (oldList != null && oldList.size() > 0) {
            for (int k = 0; k < oldList.size(); k++) {
                T1 source = oldList.get(k);
                Class<?> classType = clazz.getClass().getSuperclass();
                Object dest = new Object();
                // 获取属性
                BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class);
                PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();

                BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), Object.class);
                PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();

                for (int i = 0; i < sourceProperty.length; i++) {
                    for (int j = 0; j < destProperty.length; j++) {
                        if (sourceProperty[i].getName().equals(destProperty[j].getName()) && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) {
                            // 调用source的getter方法和dest的setter方法
                            destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
                            break;
                        }
                    }
                }
                newList.add(dest);
            }
        }
        return newList;
    }

    public static ResponseEntity<JsonResult> returnResponse(Object object) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(object);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    public static Object JsonUnit(Object a, String Key) {
        Map map = (Map) a;
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            if (entry.getKey().toString().equals(Key)) {
                return entry.getValue();
            }
        }
        return null;
    }

}
